
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define tc_log(_fmt_, ...)          printf("[TC][%s:%d][%s] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define tc_log_err(_fmt_, ...)      printf("[TC][ERR][%s:%d][%s] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)


#define DeepOfArray(a)              (sizeof(a) / sizeof(*(a)))
#define max(a, b)	(a > b ? a : b)
#define min(a, b)	(a < b ? a : b)


static void hex_dump(char *desc, unsigned int *data, int len)
{
    unsigned int i;

    printf("%s size %d hex dump:", desc, len);
    for(i=0; i<len; i++)
    {
        if (i % 16 == 0)
            printf("\n%p: ", &data[i]);

        printf(" %02x", data[i]);
    }
    printf("\n");
}

unsigned int getmax(unsigned int *in, int inlen)
{
	int i;
	unsigned int maxval = in[0];

	for (i = 1; i < inlen; ++i) {
		maxval = max(maxval, in[i]);
	}
	return maxval;
}

unsigned int getmin(unsigned int *in, int inlen)
{
	int i;
	unsigned int minval = in[0];

	for (i = 1; i < inlen; ++i) {
		minval = min(minval, in[i]);
	}
	return minval;
}

int calc(unsigned int *in, int inlen, int hour)
{
	unsigned int k, k_high, k_low, k_tmp;
	unsigned int i, total = 0;
	int debug = 0;

	hex_dump("in", in, inlen);
	tc_log("inlen %d hour %d", inlen, hour);

	if (hour < inlen)
		return -1;

	k_high = getmax(in, inlen);
	k_low = getmin(in, inlen);
	tc_log("k_high %d k_low %d", k_high, k_low);

	k = k_tmp = k_high;
	k_tmp = (k_high + k_low) >> 1;

	while (1) {
		tc_log("k_tmp %d", k_tmp);

		for (i = 0, total = 0; i < inlen; ++i) {
			total += (in[i] + k_tmp - 1)  / k_tmp;
		}

		tc_log("total %d hour %d", total, hour);
		if (total <= hour) {
			k = k_tmp;

			k_tmp = (k_tmp + k_low) >> 1;
			if (k_tmp == k_low)
				break;
		}
		else {
			// break;
			k_tmp = (k_tmp + k_high) >> 1;
		}

		tc_log("k_low %d k_high %d k_tmp %d k %d", k_low, k_high, k_tmp, k);

		if (debug++ > 10)
			break;
	}

	return k;
}

static int array_format(const char *str, const int strsize, unsigned int *array, int arraysize)
{
	char *ptr;
	int i, offset, val;

	i = 0;
	offset = 0;
	while (offset < strsize) {
		ptr = strchr(str + offset, ' ');
		if (ptr == NULL) {
			val = atoi(str + offset);
			if (val <= 0)
				return -1;

			array[i++] = val;
			break;
		}

		*ptr = '\0';
		val = atoi(str + offset);
		if (val <= 0)
			return -1;

		array[i++] = val;

		offset = (size_t)ptr - (size_t)str + 1;
	}
	return i;
}

int main(int argc, char const *argv[])
{
	unsigned int *array;
	unsigned int hour;
	int strsize, offset, arraysize, ret = -1;
	char *str, *ptr;

	str = malloc(1024);
	if (str == NULL)
		goto err1;

	gets(str);
	strsize = strlen(str);
	// tc_log("input len %d:%s", strsize, str);

	offset = 0;
	arraysize = 1;
	while (offset < strsize) {
		ptr = strchr(str + offset, ' ');
		if (ptr == NULL)
			break;

		offset = (size_t)ptr - (size_t)str + 1;
		arraysize++;
		// tc_log("ptr %p str %p offset %d arraysize %d", ptr, str, offset, arraysize);
	}

	array = malloc(sizeof(unsigned int) * arraysize);
	if (array == NULL)
		goto err2;

	ret = array_format(str, strsize, array, arraysize);
	if (ret < 0)
		goto err3;

	// hex_dump("array", array, arraysize);

	ret = calc(array, arraysize - 1, array[arraysize - 1]);
	tc_log("calc ret %d", ret);

err3:
	free(array);
err2:
	free(str);
err1:
	return ret;
}

